import WeexNativeModule from "../../common/WeexNativeModule";
import ACApi from "../../common/http/ACApi";
import StringUtils from "../../common/util/StringUtils";
import JsonFilter from "../../common/util/JsonFilter";
let instance;
let taskcode;
let loginUser = {};
class EMNewMainGraceControl{
    constructor(vue){
        instance = vue;
        this.getLoginUser();
    }
    getLoginUser(){
        let self = this;
        WeexNativeModule.getLoginUser(function (res) {
            let param = JSON.parse(res.loginUser);
            loginUser.id = param.id;
            loginUser.orgid = param.orgid;
            loginUser.name = param.username;
            self.initData();
        })

    }
    initData(){
        let self = this;
        WeexNativeModule.getParam(function (res) {
            let param = JSON.parse(res.param);
            // WeexNativeModule.alert(param)
            if(param.isJustOpen){
                instance.canSave = false;
                let data = JSON.parse(param.data);
                instance.EmergenModel.IS_ARREST = data.IS_ARREST;
                instance.EmergenModel.IS_CHANGE = data.IS_CHANGE;
                instance.EmergenModel.IS_RISE = data.IS_RISE;
                instance.EmergenModel.RISK_LAMINATION = data.RISK_LAMINATION;
                instance.EmergenModel.GRACE_VALUE = data.GRACE_VALUE;
                instance.EmergenModel.ageKey = data.ageKey;
                instance.EmergenModel.xinlvKey = data.xinlvKey;
                instance.EmergenModel.ssyaKey = data.ssyaKey;
                instance.EmergenModel.jiganKey = data.jiganKey;
                instance.EmergenModel.killipKey = data.killipKey;
                instance.ageKey = data.ageKey;
                instance.xinlvKey = data.xinlvKey;
                instance.ssyaKey = data.ssyaKey;
                instance.jiganKey = data.jiganKey;
                instance.killipKey = data.killipKey;
            }else {
                instance.canSave = true;
                taskcode = param.taskcode;
            }

            self.GRACE_VALUE();
        })
    }

    saveData(){
        if(StringUtils.isEmpty(taskcode)){
            return
        }
        let self = this;
        let content = JsonFilter.filter(instance.EmergenModel);
        content.orderId = taskcode;
        content.doctor = loginUser.name;
        content.doctorId = loginUser.id;
        content.orgCode = loginUser.orgid;
        // WeexNativeModule.alert(content)
        WeexNativeModule.addEMNode(content);
    }
    check(id){
        if(! instance.canSave){
            return;
        }
        if(id == 0){
            if(instance.EmergenModel.IS_ARREST == 1){
                instance.EmergenModel.IS_ARREST = 0;
            }else {
                instance.EmergenModel.IS_ARREST = 1;
            }
        }
        if(id == 1){
            if(instance.EmergenModel.IS_CHANGE == 1){
                instance.EmergenModel.IS_CHANGE = 0;
            }else {
                instance.EmergenModel.IS_CHANGE = 1;
            }
        }
        if(id == 2){
            if(instance.EmergenModel.IS_RISE == 1){
                instance.EmergenModel.IS_RISE = 0;
            }else {
                instance.EmergenModel.IS_RISE = 1;
            }
        }

        this.GRACE_VALUE();
    }
    graceType(type){
        if(! instance.canSave){
          return;
        }
        instance.EmergenModel.RISK_LAMINATION = type;
    }
    selectOption(type){
        let self = this;
        let parm = [];
        let defaultkey = 0;
        if(type == 0){
            defaultkey = instance.ageKey;
            for(let key in instance.ageArr){
                let value = instance.ageArr[key];
                let item = {};
                item.key = key;
                item.value = value;
                parm.push(item)
            }

        }
        if(type == 1){
            defaultkey = instance.xinlvKey;
            for(let key in instance.xinlvArr){
                let value = instance.xinlvArr[key];
                let item = {};
                item.key = key;
                item.value = value;
                parm.push(item)
            }

        }
        if(type == 2){
            defaultkey = instance.ssyaKey;
            for(let key in instance.ssyaArr){
                let value = instance.ssyaArr[key];
                let item = {};
                item.key = key;
                item.value = value;
                parm.push(item)
            }

        }
        if(type == 3){
            defaultkey = instance.jiganKey;
            for(let key in instance.jiganArr){
                let value = instance.jiganArr[key];
                let item = {};
                item.key = key;
                item.value = value;
                parm.push(item)
            }

        }
        if(type == 4){
            defaultkey = instance.killipKey;
            for(let key in instance.killipArr){
                let value = instance.killipArr[key];
                let item = {};
                item.key = key;
                item.value = value;
                parm.push(item)
            }

        }

        WeexNativeModule.selectOption(defaultkey,parm,function (res) {
            if(type == 0){
                instance.ageKey = res.selecKey;
                instance.EmergenModel.ageKey = res.selecKey;
            }
            if(type == 1){
                instance.xinlvKey = res.selecKey;
                instance.EmergenModel.xinlvKey = res.selecKey;
            }
            if(type == 2){
                instance.ssyaKey = res.selecKey;
                instance.EmergenModel.ssyaKey = res.selecKey;
            }
            if(type == 3){
                instance.jiganKey = res.selecKey;
                instance.EmergenModel.jiganKey = res.selecKey;
            }
            if(type == 4){
                instance.killipKey = res.selecKey;
                instance.EmergenModel.killipKey = res.selecKey;
            }
            self.GRACE_VALUE();
        });
    }
    GRACE_VALUE() {
        let ARREST_NUM = 0
        let CHANGE_NUM = 0
        let RISE_NUM = 0
        let num = instance.ageNum[instance.ageKey] + instance.xinlvNum[instance.xinlvKey] +
            instance.ssyaNum[instance.ssyaKey] + instance.jiganNum[instance.jiganKey] + instance.killipNum[instance.killipKey]
        if (instance.EmergenModel.IS_ARREST == 1) {
            ARREST_NUM = instance.selectNum
        }
        if (instance.EmergenModel.IS_ARREST === 0) {
            ARREST_NUM = 0
        }

        if (instance.EmergenModel.IS_CHANGE == 1) {
            CHANGE_NUM = instance.select2Num
        }
        if (instance.EmergenModel.IS_CHANGE === 0) {
            CHANGE_NUM = 0
        }

        if (instance.EmergenModel.IS_RISE == 1) {
            RISE_NUM = instance.select3Num
        }
        if (instance.EmergenModel.IS_RISE === 0) {
            RISE_NUM = 0
        }

        instance.EmergenModel.GRACE_VALUE = num + ARREST_NUM + CHANGE_NUM + RISE_NUM

        if (instance.EmergenModel.GRACE_VALUE <= 108) {
            instance.EmergenModel.RISK_LAMINATION = '4'
        } else if (instance.EmergenModel.GRACE_VALUE <= 140) {
            instance.EmergenModel.RISK_LAMINATION = '3'
        } else if (instance.EmergenModel.GRACE_VALUE > 140) {
            instance.EmergenModel.RISK_LAMINATION = '2'
        }
    }
    finish(){
        WeexNativeModule.finish();
    }
}

export default EMNewMainGraceControl;